package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.entity.PageResult;
import com.itheima.pojo.CheckGroup;
import com.itheima.service.CheckGroupService;
import entity.QueryPageBean;
import entity.Result;
import lombok.var;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author ceshi
 * @Title:检查组，就当模板来用
 * @Package
 * @Description:
 * @date 2021/7/13/001310:16
 */
@RestController
@RequestMapping("checkGroup")
public class CheckGroupController {
    @Reference
    private CheckGroupService service;

    /**
     * 增加
     */
    @RequestMapping("/add.do")
    public Result add(@RequestBody CheckGroup checkGroup, Integer[] ids) {
        try{
            service.add(checkGroup,ids);
            return new Result(true,"添加成功！");
        }catch (Exception ex){
            ex.printStackTrace();
            return new Result(false,ex.getMessage());
        }
    }

    /**
     * 删除
     */
    @RequestMapping("/delete.do")
    public Result delete(Integer id) {
        try{
            service.delete(id);
            return new Result(true,"删除成功！");
        }catch (Exception ex){
            ex.printStackTrace();
            return new Result(false,ex.getMessage());
        }

    }

    /**
     * 分页查询
     * @return
     */
    @RequestMapping("/getPage.do")
    public PageResult findByPage(@RequestBody QueryPageBean queryPageBean) {
        return service.findByPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize(), queryPageBean.getQueryString());
    }

    /**
     * 查询
     */
    @RequestMapping("/get.do")
    public CheckGroup findById(Integer id) {
        return service.findById(id);
    }
    /**
     * 查询检查组
     */
    @RequestMapping("/getCheckItem.do")
    public CheckGroup getCheckItem(Integer id) {
        return service.findById(id);
    }
    /**
     * 查询所有
     * @return
     */
    @RequestMapping("/findAll.do")
    public List<CheckGroup> findAll() {
        return service.findAll();
    }

    /**
     * 修改
     * @return
     */
    @RequestMapping("/update.do")
    public Result edit(@RequestBody CheckGroup checkGroup, Integer[] ids) {
        try{
            var model = service.findById(checkGroup.getId());
            if(model==null)
                return new Result(false,"对象不存在");

            service.update(checkGroup,ids);
            return new Result(true,"编辑成功");
        }catch (Exception ex){
            ex.printStackTrace();
            return new Result(false,"编辑失败");
        }
    }

}
